package chapter13;

import java.io.*;
import java.net.URL;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2018-03-25 20:46
 */
public class PropertiesTest {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("BJ", "北京");
        properties.setProperty("TJ", "天津");
        System.out.println(properties.getProperty("BJ"));
        System.out.println(properties.getProperty("TJ"));
        System.out.println(properties.getProperty("Tc", "没有此记录"));

        File file = new File("J:" + File.separator + "test.dat");

        try (OutputStream out = new FileOutputStream(file)) {
            properties.store(out, "Area info");
        } catch (IOException e) {
            e.printStackTrace();
        }

        URL url = Thread.currentThread().getContextClassLoader().getResource("app.properties");
        File profile = new File(url.getFile());
        Properties pro = new Properties();
        try (InputStream in = new FileInputStream(profile)) {
            pro.load(in);
            System.out.println(pro.getProperty("db.url"));
            System.out.println(pro.getProperty("db.username"));
            System.out.println(pro.getProperty("db.password"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
